<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blogs/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="性能优化,">










<meta name="keywords" content="性能优化">
<meta property="og:type" content="article">
<meta property="og:title" content="HTTP协议">
<meta property="og:url" content="https://wangzichong.gitee.io/blogs/2019/07/04/t-性能优化/http/index.html">
<meta property="og:site_name" content="Reading Notes">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1S5oue2WG3KVjSZFg762TspXa8.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1O6.idQxz61VjSZFr760eLFXaB.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1KjKoXkH0gK0jSZPi761vapXag.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1r1qnXfb2gK0jSZK9761EgFXar.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1Zd6lXeT2gK0jSZFv760nFXXae.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1D8jlXX67gK0jSZPf761hhFXa9.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1ZI_qXoY1gK0jSZFC763wqXXaK.png">
<meta property="og:image" content="https://ae01.alicdn.com/kf/HTB1VN2yXhz1gK0jSZSg761vwpXa6.png">
<meta property="og:updated_time" content="2019-10-15T13:10:05.787Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HTTP协议">
<meta name="twitter:image" content="https://ae01.alicdn.com/kf/HTB1S5oue2WG3KVjSZFg762TspXa8.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blogs/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://wangzichong.gitee.io/blogs/2019/07/04/t-性能优化/http/">





  <title>HTTP协议 | Reading Notes</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Reading Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">Codes Change Life</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blogs/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blogs/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            Search
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://wangzichong.gitee.io/blogs/blogs/2019/07/04/t-性能优化/http/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zichong">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blogs/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reading Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">HTTP协议</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-07-04T21:13:49+08:00">
                2019-07-04
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blogs/categories/学习笔记/" itemprop="url" rel="index">
                    <span itemprop="name">学习笔记</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          
              <div class="post-description">
                   
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <a id="more"></a>
<h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>HTTP即超文本传输协议(应用层协议)<br>端口号: http -&gt; 80; https -&gt; 443</p>
<h3 id="HTTP请求模型"><a href="#HTTP请求模型" class="headerlink" title="HTTP请求模型"></a>HTTP请求模型</h3><img src="https://ae01.alicdn.com/kf/HTB1S5oue2WG3KVjSZFg762TspXa8.png" width="80%" height="80%" alt="client-server" title="client-server">

<h3 id="浏览器行为与HTTP协议"><a href="#浏览器行为与HTTP协议" class="headerlink" title="浏览器行为与HTTP协议"></a>浏览器行为与HTTP协议</h3><h4 id="处理流程"><a href="#处理流程" class="headerlink" title="处理流程"></a>处理流程</h4><ol>
<li>输入google.com并回车</li>
<li>解析域名(DNS服务器将域名解析成对应的IP地址)</li>
<li>浏览器发送HTTP请求</li>
<li>(找到对应的服务器)服务器收到请求, 并处理</li>
<li>服务器返回HTML相应</li>
<li>浏览器处理HTML页面</li>
<li>继续请求其他资源<img src="https://ae01.alicdn.com/kf/HTB1O6.idQxz61VjSZFr760eLFXaB.png" width="80%" height="80%" alt="progress" title="progress">

</li>
</ol>
<h3 id="HTTP工作过程"><a href="#HTTP工作过程" class="headerlink" title="HTTP工作过程"></a>HTTP工作过程</h3><h4 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h4><p>完成一件事件分为若干个步骤, 同时这若干个步骤要有严格的先后执行顺序, 而且只要其中一个步骤失败, 则整个事件都算失败.</p>
<h4 id="工作过程"><a href="#工作过程" class="headerlink" title="工作过程"></a>工作过程</h4><p>一次HTTP操作称为一个<strong>事务</strong>, 可以分为四步</p>
<ol>
<li>客户机与服务器建立连接, 只要单击某个超链接, HTTP则开始工作(在TCP层面上)</li>
<li>建立连接后, 客户机发送一个请求给服务器(在HTTP层面上的request请求)</li>
<li>服务器接收到请求后, 给予相应的响应信息(在HTTP层面上的response响应)</li>
<li>客户端接收服务器所返回的信息, 通过浏览器来渲染到显示屏上, 然后客户机与服务器断开连接(在TCP层面上断开)</li>
</ol>
<h3 id="请求与响应"><a href="#请求与响应" class="headerlink" title="请求与响应"></a>请求与响应</h3><ul>
<li>HTTP请求组成: 请求行, 消息报头, 请求正文</li>
<li>HTTP响应组成: 状态行, 消息报头, 响应正文<ul>
<li>在Chrome调试工具中<ul>
<li>将请求行与状态行统一放置在General中(协议信息被忽略) </li>
<li>Request Headers -&gt; 请求头</li>
<li>Response Headers -&gt; 响应头(可以自定义一些响应报头, key: value)</li>
<li>Query String Parameters -&gt; post的数据<img src="https://ae01.alicdn.com/kf/HTB1KjKoXkH0gK0jSZPi761vapXag.png" width="80%" height="80%" alt="request" title="request">
<img src="https://ae01.alicdn.com/kf/HTB1r1qnXfb2gK0jSZK9761EgFXar.png" width="80%" height="80%" alt="response" title="response">

</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="请求方法"><a href="#请求方法" class="headerlink" title="请求方法"></a>请求方法</h3><ul>
<li>GET(查): 请求获取Request-URI所标识的资源</li>
<li>POST(改): 在Request-URI所标识的资源后附加新的数据</li>
<li>HEAD: 请求获取由Request-URI所标识的资源的响应消息报头</li>
<li>PUT(增): 请求服务器存储一个资源, 并用Request-URI作为其标识 </li>
<li>DELETE(删): 请求服务器删除Request-URI所标识的资源</li>
<li>TRACE: 请求服务器回送收到的请求信息, 主要用于测试或诊断</li>
<li>CONNECT: HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器</li>
<li>OPTIONS: 请求查询服务器的性能，或者查询与资源相关的选项和需求</li>
</ul>
<h3 id="HTTP状态码"><a href="#HTTP状态码" class="headerlink" title="HTTP状态码"></a>HTTP状态码</h3><p>状态代码有三位数字组成，第一个数字定义了响应的类别，且有五种可能取值:</p>
<ul>
<li>1xx: 指示信息–表示请求已接收，继续处理 </li>
<li>2xx: 成功–表示请求已被成功接收、理解、接受 </li>
<li>3xx: 重定向–要完成请求必须进行更进一步的操作 </li>
<li>4xx: 客户端错误–请求有语法错误或请求无法实现 </li>
<li>5xx: 服务器端错误–服务器未能实现合法的请求</li>
</ul>
<h3 id="常用的请求报头"><a href="#常用的请求报头" class="headerlink" title="常用的请求报头"></a>常用的请求报头</h3><ul>
<li>Accept: 用于指定客户端接受哪些类型的信息 eg: Accept:image/gif，Accept:text/ html</li>
<li>Accept-Charset: 用于指定客户端接受的字符集</li>
<li>Accept-Encoding: 类似于Accept, 但是它是用于指定可接受的内容编码</li>
<li>Accept-Language: 类似于Accept，但是它是用于指定一种自然语言</li>
<li>Authorization: 用于证明客户端有权查看某个资源. 当浏览器访问一个页面时, 如果收到服务器的响应代码为401(未授权), 可以发送一个包含Authorization请求报头域的请求, 要求服务器对其进行验证</li>
<li>Host: 用于指定被请求资源的Internet主机和端口号, 它通常从HTTP URL中提取出来的, 发送请求时, 该报头域是必需的</li>
<li>User-Agent: 允许客户端将它的操作系统, 浏览器和其它属性告诉服务器</li>
</ul>
<h3 id="常用的响应报头"><a href="#常用的响应报头" class="headerlink" title="常用的响应报头"></a>常用的响应报头</h3><ul>
<li>Location: 用于重定向接受者到一个新的位置, 常用在更换域名的时候</li>
<li>Server: 它包含了服务器用来处理请求的软件信息, 与User-Agent请求报头域是相对应的</li>
<li>WWW-Authenticate: 它必须被包含在401(未授权的)响应消息中, 客户端收到401响应消息时候, 并发送Authorization报头域请求服务器对其进行验证时, 服务端响应报头就包含该报头域 </li>
</ul>
<h3 id="常用的实体报头"><a href="#常用的实体报头" class="headerlink" title="常用的实体报头"></a>常用的实体报头</h3><p>定义被传输资源的信息, 可以用于请求, 也可以用于响应</p>
<ul>
<li>Content-Encoding: 被用作媒体类型的修饰符, 它的值指示了已经被应用到实体正文的附加内容的编码, 因而要获得Content-Type报头域中所引用的媒体类型, 必须采用相应的解码机制</li>
<li>Content-Language: 描述了资源所用的自然语言</li>
<li>Content-Length: 用于指明实体正文的长度, 以字节方式存储的十进制数字来表示</li>
<li>Content-Type: 用于指明发送给接收者的实体正文的媒体类型</li>
<li>Last-Modified: 用于指示资源的最后修改日期和时间</li>
<li>Expires: 给出响应过期的日期和时间</li>
</ul>
<h3 id="Cookies与Session"><a href="#Cookies与Session" class="headerlink" title="Cookies与Session"></a>Cookies与Session</h3><ul>
<li>Cookies是保存在客户端的<strong>小段文本</strong>(可以在服务端生成, 也可以在客户端生成), 随客户端点每一个请求(浏览器自动附加cookie)发送该url下的所有cookies到服务器端</li>
<li>Session则保存在服务器端, 本质是<strong>特殊的数据库</strong>(只能在服务端生成), 通过唯一的值sessionID来区别每一个用户, sessionID随每个连接请求发送到服务器, 服务器根据sessionID来识别客户端, 再通过session的key获取session值<h4 id="Cookie与Session的使用"><a href="#Cookie与Session的使用" class="headerlink" title="Cookie与Session的使用"></a>Cookie与Session的使用</h4></li>
<li>浏览器第一次访问服务器(请求中是没有Cookie信息的)</li>
<li>服务器收到请求的时候, 检查请求中是否附加cookie, 如果没有, 服务器会生成一个cookie(Set-Cookie: JSESSIONID=xxxx)附加到response header里, 然后这个Cookie里面的JSESSIONID相关的信息会被服务器记录下来</li>
<li>客户端再一次向服务器发送请求的时候, 浏览器会默认将cookie附加到request header上</li>
<li>服务器接收到请求的时候, 会从JSESSIONID=xxxx里面取得xxxx(索引), 去服务器里寻找相关信息, 从而实现会话的保持(因为HTTP是一个无状态的协议, 要依靠Cookie与Session来维护登陆状态, 如果Cookie被禁, 可以使用<strong>URL回显来实现</strong>)<img src="https://ae01.alicdn.com/kf/HTB1Zd6lXeT2gK0jSZFv760nFXXae.png" width="80%" height="80%" alt="cookie&session" title="cookie&session">

</li>
</ul>
<h3 id="HTTP缓存机制"><a href="#HTTP缓存机制" class="headerlink" title="HTTP缓存机制"></a>HTTP缓存机制</h3><p>在本地磁盘上有个用于浏览器缓存的空间(空间有大小限制), 缓存会根据请求保存输出内容的副本, 例如html页面, 图片, 文件, 当下一个请求来到的时候: 如果是相同的URL, 缓存直接使用副本响应访问请求, 而不是向源服务器再次发送请求, 从而<strong>减少响应延迟, 减少网络带宽消耗</strong><br><img src="https://ae01.alicdn.com/kf/HTB1D8jlXX67gK0jSZPf761hhFXa9.png" width="80%" height="80%" alt="cache-1" title="cache"></p>
<h4 id="强制缓存与协商缓存-对比缓存"><a href="#强制缓存与协商缓存-对比缓存" class="headerlink" title="强制缓存与协商缓存(对比缓存)"></a>强制缓存与协商缓存(对比缓存)</h4><ul>
<li>强制缓存: 服务器通知浏览器一个缓存时间, 在缓存时间内, 下次请求直接用缓存, 不在时间内, 执行比较缓存策略</li>
<li>比较缓存: 将缓存信息中的Etag和Last-Modified通过请求发送给服务器, 由服务器校验, 返回304状态码时, 浏览器直接使用缓存<img src="https://ae01.alicdn.com/kf/HTB1ZI_qXoY1gK0jSZFC763wqXXaK.png" width="80%" height="80%" alt="cache-2" title="cache">

</li>
</ul>
<h3 id="HTTP2-协议分析"><a href="#HTTP2-协议分析" class="headerlink" title="HTTP2 协议分析"></a>HTTP2 协议分析</h3><ul>
<li>使用二进制格式传输, 更高效更紧凑</li>
<li>对报头压缩, 降低开销</li>
<li>多路复用, 一个网络连接实现并行请求</li>
<li>服务器主动推送, 减少请求的延迟</li>
<li>默认使用加密</li>
</ul>
<h3 id="HTTP与反向代理"><a href="#HTTP与反向代理" class="headerlink" title="HTTP与反向代理"></a>HTTP与反向代理</h3><h4 id="正向代理"><a href="#正向代理" class="headerlink" title="正向代理"></a>正向代理</h4><p>proxy(代理服务器)替代访问方(Client)去访问目标服务器(Server)的过程, 局域网内 -&gt; 局域网外</p>
<ul>
<li>监控, 限制指定网站的访问 -&gt; 所有的Client(流量)都要通过proxy到达外网(Server)</li>
<li>缓存(节省出口带宽) -&gt; 第一个用户(电脑)通过proxy访问到一个服务器, 请求的data可以暂时存储在proxy, 另外一台电脑同样访问到这个服务器, 则可以通过proxy直接返回暂时存储的data</li>
</ul>
<h4 id="反向代理"><a href="#反向代理" class="headerlink" title="反向代理"></a>反向代理</h4><p>与正向代理正好相反, 对于Client而言, proxy就是一个原始服务器, proxy接收到请求后, 根据url的规则, 将请求转发到对应的服务器, 并将获得的data返回给Client</p>
<ul>
<li>外网发布(eg: 对外统一80端口), 请求转发, 从而实现一个网络出口(只有一个IP), 对应多个Server</li>
<li>加密和SSL加速</li>
<li>负载均衡</li>
<li>缓存静态内容(图片, css, 脚本等, 一般很少发生变动)</li>
<li>压缩(gzip)</li>
<li>安全(安全防线)<img src="https://ae01.alicdn.com/kf/HTB1VN2yXhz1gK0jSZSg761vwpXa6.png" width="80%" height="80%" alt="proxy" title="proxy">

</li>
</ul>
<h3 id="nginx安装与使用"><a href="#nginx安装与使用" class="headerlink" title="nginx安装与使用"></a>nginx安装与使用</h3><ul>
<li>centos7下 下载与安装 nginx<ul>
<li>sudo rpm -ivh <a href="http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm" target="_blank" rel="noopener">http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</a></li>
<li>sudo yum repolist</li>
<li>sudo yum install nginx</li>
</ul>
</li>
<li>cd到/etc/nginx/目录下<ul>
<li>打开nginx.conf配置文件进行配置: vi nginx.conf<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#配置转发</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#upstream关键字, 定义转发的服务器, eg: 8080/8090两个地址代表2个node server</span></span><br><span class="line">upstream web_chong &#123;</span><br><span class="line">  server 127.0.0.1:8080;</span><br><span class="line">  <span class="comment"># 如果ngnix与node server不在一个服务器上, 需要写具体的ip地址</span></span><br><span class="line">&#125;</span><br><span class="line">upstream web_Sys &#123;</span><br><span class="line">  server 127.0.0.1:8090;</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    server_name localhost;</span><br><span class="line">    location /chong/ &#123;</span><br><span class="line">      <span class="comment">#请求的url中含有/chong/的关键字, 就转发到8080端口下的/chong/路径下</span></span><br><span class="line">      proxy_pass http://web_chong/chong/;</span><br><span class="line">      proxy_redirect off;</span><br><span class="line">    &#125;</span><br><span class="line">    location /Sys/ &#123;</span><br><span class="line">      proxy_pass http://web_Sys/Sys/;</span><br><span class="line">      proxy_redirect off;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/性能优化/" rel="tag"># 性能优化</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2019/07/02/t-Promise/promise/" rel="next" title="Promise">
                <i class="fa fa-chevron-left"></i> Promise
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2019/07/06/t-习题集/test-paper-5/" rel="prev" title="自测习题集(5)">
                自测习题集(5) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">zichong</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blogs/categories/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#定义"><span class="nav-number">1.</span> <span class="nav-text">定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP请求模型"><span class="nav-number">2.</span> <span class="nav-text">HTTP请求模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#浏览器行为与HTTP协议"><span class="nav-number">3.</span> <span class="nav-text">浏览器行为与HTTP协议</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#处理流程"><span class="nav-number">3.1.</span> <span class="nav-text">处理流程</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP工作过程"><span class="nav-number">4.</span> <span class="nav-text">HTTP工作过程</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#事务"><span class="nav-number">4.1.</span> <span class="nav-text">事务</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#工作过程"><span class="nav-number">4.2.</span> <span class="nav-text">工作过程</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请求与响应"><span class="nav-number">5.</span> <span class="nav-text">请求与响应</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#请求方法"><span class="nav-number">6.</span> <span class="nav-text">请求方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP状态码"><span class="nav-number">7.</span> <span class="nav-text">HTTP状态码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常用的请求报头"><span class="nav-number">8.</span> <span class="nav-text">常用的请求报头</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常用的响应报头"><span class="nav-number">9.</span> <span class="nav-text">常用的响应报头</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常用的实体报头"><span class="nav-number">10.</span> <span class="nav-text">常用的实体报头</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Cookies与Session"><span class="nav-number">11.</span> <span class="nav-text">Cookies与Session</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Cookie与Session的使用"><span class="nav-number">11.1.</span> <span class="nav-text">Cookie与Session的使用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP缓存机制"><span class="nav-number">12.</span> <span class="nav-text">HTTP缓存机制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#强制缓存与协商缓存-对比缓存"><span class="nav-number">12.1.</span> <span class="nav-text">强制缓存与协商缓存(对比缓存)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP2-协议分析"><span class="nav-number">13.</span> <span class="nav-text">HTTP2 协议分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP与反向代理"><span class="nav-number">14.</span> <span class="nav-text">HTTP与反向代理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#正向代理"><span class="nav-number">14.1.</span> <span class="nav-text">正向代理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#反向代理"><span class="nav-number">14.2.</span> <span class="nav-text">反向代理</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nginx安装与使用"><span class="nav-number">15.</span> <span class="nav-text">nginx安装与使用</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zichong</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blogs/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
